package headfirst.combined.djview;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Component;
import java.awt.Dimension;
import java.awt.EventQueue;

import javax.sound.sampled.AudioSystem;
import javax.sound.sampled.Clip;
import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JButton;
import javax.swing.JProgressBar;

import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.geom.Point2D;
import java.io.File;
import java.awt.Panel;
import java.awt.Font;
import javax.swing.JToggleButton;

public class MusicMixPad {

	private static JFrame frame;
	private static JButton botonP,botonB,botonR;
	private JButton btnDo;
	private JButton btnRe;
	private JButton btnMi;
	private JButton btnFa;
	private JButton btnSol;
	private JButton btnLa;
	private JButton button_6;
	private JButton button_7;
	private JButton button_8;
	private JButton button_9;
	private JButton btnSi;
	private JButton button;
	private JProgressBar progressBar_1;
	private JProgressBar progressBar_2;
	private JButton btnStart;
	private JButton btnStop;
	private JToggleButton botonManual;
	
	public static void main(String []args){
		EventQueue.invokeLater(new Runnable() {
			public void run() {
				try {
					MusicMixPad window = new MusicMixPad();
					window.frame.setVisible(true);
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		});
	}
		
	public MusicMixPad (){
		initialize();
	}
			
	private void initialize() {		
		  // Create and set up the window.
		  frame = new JFrame("DJ TEST DRIVE MIX PAD CENTER");
		  frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		 
		  TransparentPanel panel = new TransparentPanel();
		  panel.setBackgroundImage(new ImageIcon("fondo2.jpg").getImage());
		  
		  Component contents = panel;
		  frame.getContentPane().add(contents, BorderLayout.CENTER);
		  panel.setLayout(null);
		 
		  /*
		  botonManual = new JButton("Modo Pad");
		  panel.add(botonManual);
			botonManual.addKeyListener(new KeyAdapter() {
				@Override
				public void keyPressed(KeyEvent tecla) {
					if(tecla.VK_A==tecla.getKeyCode()){
						botonB.setBackground(Color.RED);
						playBombo();
					}
					if(tecla.VK_S==tecla.getKeyCode()){
						botonP.setBackground(Color.BLUE);
						playPlatillo();
					}
					if(tecla.VK_D==tecla.getKeyCode()){
						botonR.setBackground(Color.GREEN);
						playRedoblante();
					}
				}
				@Override
				public void keyReleased(KeyEvent tecla) {
					if(tecla.VK_A==tecla.getKeyCode()){
						botonB.setBackground(null);
					}
					if(tecla.VK_S==tecla.getKeyCode()){
						botonP.setBackground(null);
					}
					if(tecla.VK_D==tecla.getKeyCode()){
						botonR.setBackground(null);
					}
				}
			});
			botonManual.setBounds(20, 155, 120, 54);
			*/

			//////////////////////////////////////////////////////////////////////////////
			//Botones y Acciones
			botonB = new JButton("Bombo");
			botonB.addActionListener(new ActionListener() {
				public void actionPerformed(ActionEvent arg0) {
					//Crear instrumento
					//Reproducir mientras queda apretado
					//Stopear al apagar
					playBombo();
				}
			});
			panel.add(botonB);
			botonB.addMouseListener(new MouseAdapter() {
				@Override
				public void mouseClicked(MouseEvent arg0) {
					
				}
			});
			botonB.setBounds(250, 262, 89, 54);
			
			
			
			
			botonP = new JButton("Platillo");
			botonP.addActionListener(new ActionListener() {
				public void actionPerformed(ActionEvent arg0) {
				}
			});
			panel.add(botonP);
			botonP.addMouseListener(new MouseAdapter() {
				@Override
				public void mouseClicked(MouseEvent arg0) {
					playPlatillo();
				}
			});
			botonP.setBounds(349, 262, 89, 54);
			
			
			botonR = new JButton("Redoblante");
			panel.add(botonR);
			botonR.addMouseListener(new MouseAdapter() {
				@Override
				public void mouseClicked(MouseEvent arg0) {
					playRedoblante();
				}
			});
			botonR.setBounds(448, 262, 87, 53);
			
			btnDo = new JButton("DO");
			btnDo.setFont(new Font("Tahoma", Font.PLAIN, 8));
			btnDo.setBackground(Color.WHITE);
			btnDo.setBounds(105, 369, 48, 107);
			panel.add(btnDo);
			btnDo.addKeyListener(new KeyAdapter() {
				@Override
				public void keyPressed(KeyEvent tecla) {
					if(tecla.VK_Z==tecla.getKeyCode()){
						//botonB.setBackground(Color.RED);
						//Boton Z presionado
					}
				}});
			btnRe = new JButton("RE");
			btnRe.setFont(new Font("Tahoma", Font.PLAIN, 8));
			btnRe.setBackground(Color.WHITE);
			btnRe.setBounds(178, 369, 47, 107);
			panel.add(btnRe);
			
			btnMi = new JButton("MI");
			btnMi.setFont(new Font("Tahoma", Font.PLAIN, 8));
			btnMi.setBackground(Color.WHITE);
			btnMi.setBounds(250, 369, 47, 107);
			panel.add(btnMi);
			
			btnFa = new JButton("FA");
			btnFa.setFont(new Font("Tahoma", Font.PLAIN, 8));
			btnFa.setBackground(Color.WHITE);
			btnFa.setBounds(296, 369, 47, 107);
			panel.add(btnFa);
			
			btnSol = new JButton("Sol");
			btnSol.setFont(new Font("Tahoma", Font.PLAIN, 8));
			btnSol.setBackground(Color.WHITE);
			btnSol.setBounds(368, 369, 47, 107);
			panel.add(btnSol);
			
			btnLa = new JButton("LA");
			btnLa.setFont(new Font("Tahoma", Font.PLAIN, 8));
			btnLa.setBackground(Color.WHITE);
			btnLa.setBounds(440, 369, 47, 107);
			panel.add(btnLa);
			
			button_6 = new JButton("");
			button_6.setBackground(Color.BLACK);
			button_6.setBounds(342, 369, 27, 51);
			panel.add(button_6);
			
			button_7 = new JButton("");
			button_7.setBackground(Color.BLACK);
			button_7.setBounds(152, 369, 27, 51);
			panel.add(button_7);
			
			button_8 = new JButton("");
			button_8.setBackground(Color.BLACK);
			button_8.setBounds(224, 369, 27, 51);
			panel.add(button_8);
			
			button_9 = new JButton("");
			button_9.setBackground(Color.BLACK);
			button_9.setBounds(414, 369, 27, 51);
			panel.add(button_9);
			
			btnSi = new JButton("SI");
			btnSi.setFont(new Font("Tahoma", Font.PLAIN, 8));
			btnSi.setBackground(Color.WHITE);
			btnSi.setBounds(513, 369, 47, 107);
			panel.add(btnSi);
			
			button = new JButton("");
			button.setBackground(Color.BLACK);
			button.setBounds(486, 369, 27, 51);
			panel.add(button);
			
			JProgressBar progressBar = new JProgressBar();
			  progressBar.setBounds(250, 228, 89, 23);
			  panel.add(progressBar);
			  
			  progressBar_1 = new JProgressBar();
			  progressBar_1.setBounds(349, 228, 89, 23);
			  panel.add(progressBar_1);
			  
			  progressBar_2 = new JProgressBar();
			  progressBar_2.setBounds(446, 228, 89, 23);
			  panel.add(progressBar_2);
			  
			  btnStart = new JButton("Start");
			  btnStart.setBounds(121, 243, 89, 23);
			  panel.add(btnStart);
			  
			  btnStop = new JButton("Stop");
			  btnStop.setBounds(121, 278, 89, 23);
			  panel.add(btnStop);
			  
			  JLabel lblNewLabel = new JLabel("Roland SPD-SX Sampling Pad");
			  lblNewLabel.setForeground(Color.WHITE);
			  lblNewLabel.setFont(new Font("Tahoma", Font.PLAIN, 37));
			  lblNewLabel.setBounds(166, 28, 508, 83);
			  panel.add(lblNewLabel);
			  
			  botonManual = new JToggleButton("Pad Mode");
			  
			  //botonManual = new JButton("Modo Pad");
			  //panel.add(botonManual);
			  botonManual.addKeyListener(new KeyAdapter() {
					@Override
					public void keyPressed(KeyEvent tecla) {
						if(tecla.VK_A==tecla.getKeyCode()){
							botonB.setBackground(Color.RED);
							playBombo();
						}
						if(tecla.VK_S==tecla.getKeyCode()){
							botonP.setBackground(Color.BLUE);
							playPlatillo();
						}
						if(tecla.VK_D==tecla.getKeyCode()){
							botonR.setBackground(Color.GREEN);
							playRedoblante();
						}
					}
					@Override
					public void keyReleased(KeyEvent tecla) {
						if(tecla.VK_A==tecla.getKeyCode()){
							botonB.setBackground(null);
						}
						if(tecla.VK_S==tecla.getKeyCode()){
							botonP.setBackground(null);
						}
						if(tecla.VK_D==tecla.getKeyCode()){
							botonR.setBackground(null);
						}
					}
				});
				///botonManual.setBounds(20, 155, 120, 54);
				botonManual.setBounds(10, 11, 98, 68);
				panel.add(botonManual);
			
			
			
			
			
			//Fin Botones y Acciones
			////////////////////////////////////////////////////////////////////////////
			
			//Set window size
			frame.setPreferredSize(new Dimension(700,525));
			 
			// Display the window.
			frame.pack();
			//frame.setLocationByPlatform(true);
			frame.setLocation(180,100);
			frame.setVisible(true);
			
		}
		
		
		
		
		public static void playBombo(){
			try {
				Clip sonido = AudioSystem.getClip();
				File a = new File("sonidos\\wavB.wav");
				sonido.open(AudioSystem.getAudioInputStream(a));
				sonido.start();
				System.out.println("Dum");
				//Thread.sleep(2000); // 1000 milisegundos (1 segundos)
				//sonido.close();
				}
				catch (Exception tipoerror) {
				System.out.println("" + tipoerror);
				}
		}
		
		public static void playPlatillo(){
			try {
				Clip sonido = AudioSystem.getClip();
				File a = new File("sonidos\\wavP.wav");
				sonido.open(AudioSystem.getAudioInputStream(a));
				sonido.start();
				System.out.println("Tss");
				//Thread.sleep(1200); // 1000 milisegundos (1 segundos)
				//sonido.close();
				}
				catch (Exception tipoerror) {
				System.out.println("" + tipoerror);
				}
		}
		
		public static void playRedoblante(){
			try {
				Clip sonido = AudioSystem.getClip();
				File a = new File("sonidos\\wavR.wav");
				sonido.open(AudioSystem.getAudioInputStream(a));
				sonido.start();
				System.out.println("Tash");
				//Thread.sleep(2000); // 1000 milisegundos (1 segundos)
				//sonido.close();
				}
				catch (Exception tipoerror) {
				System.out.println("" + tipoerror);
				}
		}
}
